# coding:utf-8
# 通用工具包

from werkzeug.routing import BaseConverter
from iHome.utils.response_code import RET
from flask import session, jsonify, g
import functools


class RegexConverter(BaseConverter):
    """自定义转换器"""
    def __init__(self, url_map, *args):
        super(RegexConverter, self).__init__(url_map)
        self.regex = args[0]


def login_required(f):
    """用户登录状态装饰器"""
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        if not user_id:
            return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
        else:
            g.user_id = user_id
            return f(*args, **kwargs)
    return wrapper
